<?php


namespace app\admin\model;


use app\admin\model\Banner as BannerModel;
use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use oss\Oss;

class Banner extends BaseModel {

    public static $where = [
        ['key' => 1, 'value' => '首页'],
        ['key' => 2, 'value' => '房源页'],
        ['key' => 3, 'value' => '名师讲堂页'],
        ['key' => 4, 'value' => '系统消息'],
        ['key' => 5, 'value' => '雷达'],
    ];

    /**
     * 列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $map = [];
        if (!empty($params['seat'])) {
            $map[] = ['seat', '=', (int)$params['seat']];
        }
        $count = self::where($map)->count();
        if (empty($count)) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
        } else {
            $page = empty($params['page']) ? 1 : intval($params['page']);
            $long = empty($params['long']) ? 10 : intval($params['long']);
            $data = self::page($page, $long)
                ->order('sort', 'asc')
                ->where($map)
                ->select()
                ->toArray();
            $pageNum = ceil($count / $long);
            $result = [
                'page'    => $page,
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
        }
        return $result;
    }

    /**
     * 排序
     * @param $params
     * @throws \Exception
     */
    public static function editSort($params) {
        $ids = explode(',', $params['ids']);
        if ($ids) {
            $count = count($ids);
            $int = 1;
            for ($i = 0; $i < $count; $i++) {
                $arr = [
                    'id'   => $ids[$i],
                    'sort' => $int
                ];
                $list[] = $arr;
                $int++;
            }
            $region = new self();
            $region->saveAll($list);
        }
    }

    /**
     * 编辑、添加
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function post($params) {
        if (!empty($params['id'])) {
            if (!$old = self::where('id', $params['id'])->select()->toArray()) {
                Err::add('数据不存在', RespCode::PARAMS_ERR);
                return false;
            }
        }
        if (!empty($params['link'])) {
            if (mb_strlen(trim($params['link']), 'utf-8') > 200) {
                Err::add('链接长度限制200', RespCode::PARAMS_ERR);
                return false;
            }
        }
        if(empty($params['id'])){
            if (in_array($params['seat'], [1])) {
                if (self::where('seat', $params['seat'])->count() >= 5) {
                    Err::add('首页banner最多添加5张图片', RespCode::PARAMS_ERR);
                    return false;
                }
            }
            if (!in_array($params['seat'], [1])) {
                if (self::where('seat', $params['seat'])->count() >= 1) {
                    Err::add('房源页、名师讲堂广告图、雷达广告图、系统消息最多一张图片', RespCode::PARAMS_ERR);
                    return false;
                }
            }
        }else{
            $old=self::where('id', $params['id'])->value('seat');
            if($old!=$params['seat'] ){
                if (in_array($params['seat'], [1])) {
                    if (self::where('seat', $params['seat'])->count() >= 5) {
                        Err::add('首页banner最多添加5张图片', RespCode::PARAMS_ERR);
                        return false;
                    }
                }
                if (!in_array($params['seat'], [1])) {
                    if (self::where('seat', $params['seat'])->count() >= 1) {
                        Err::add('房源页、名师讲堂广告图、雷达广告图、系统消息最多一张图片', RespCode::PARAMS_ERR);
                        return false;
                    }
                }
            }
        }
        $data = [
            'url'    => trim($params['url']),
            'link'   => $params['link'] ?? '',
            'seat'   => $params['seat'],
            'status' => 1,
        ];
        if (empty($params['id'])) {
            $data['itime'] = time();
            self::insert($data);
        } else {
            //删除图片
            if ($old[0]['url'] != $params['url'] && !empty($old[0]['url'])) {
                $oss = Oss::getInstance();
                $oss->delOneFile($old[0]['url']);
            }
            self::where('id', $params['id'])->update($data);
        }
        return true;
    }

    /**
     * 删除
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function del($params) {
        if (!$old = self::where('id', $params['id'])->select()->toArray()) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        //删除图片
        if (self::where('id', $params['id'])->delete()) {
            $oss = Oss::getInstance();
            $oss->delOneFile($old[0]['url']);
        }
        return true;
    }

    /**
     * 详情
     * @param $id
     * @param $_info
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function info($id, &$_info) {
        $info = self::where('id', $id)
            ->select()
            ->toArray();
        if (empty($info)) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $_info = $info[0];
        return true;
    }
}
